﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.User
{
    public partial class editPurchase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorEditPurchaseDescription.Width = 450;
                this.Form.DefaultButton = btnEditPurchase.UniqueID;

                PopulateCategoryDropDown();
                BindData();
                BindSubMenu();
                lblConfirmUpdatedPurchase.Visible = false;

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkProfile.NavigateUrl = "~/User/profile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkEditProfile.NavigateUrl = "~/User/editProfile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkChangePassword.NavigateUrl = "~/User/changePassword.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSettings.NavigateUrl = "~/User/editSettings.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSale.NavigateUrl = "~/User/userSale.aspx?uid=" + Request.QueryString["uid"];
            hyLinkPurchases.NavigateUrl = "~/User/userPurchase.aspx?uid=" + Request.QueryString["uid"];
        }

        /// <summary>
        /// Populates the Category DropDownList.
        /// </summary>
        private void PopulateCategoryDropDown()
        {
            List<Category> categories = dbExchange.GetAllCategories();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (categories.Count > 0)
            {
                ddlEditPurchaseCategory.DataSource = categories;
                if(!culture.Equals("en-GB"))
                    ddlEditPurchaseCategory.DataTextField = "CategoryName";
                else
                    ddlEditPurchaseCategory.DataTextField = "LocalizedCategoryName";

                ddlEditPurchaseCategory.DataValueField = "CategoryID";
                ddlEditPurchaseCategory.DataBind();
            }
        }

        /// <summary>
        /// Edits a purchase based on the input parameters.
        /// </summary>
        /// <param name="title">Specifies the title to be used in the edit.</param>
        /// <param name="description">Specifies the description to be used in the edit.</param>
        /// <param name="categoryId">Specifies the ID of the category to be used in the edit.</param>
        /// <param name="isActive">Specifies whether or not the purchase should be active or not.</param>
        /// <param name="purchaseId">Specifies the ID of the purchase to be edited.</param>
        private void EditPurchase(string title, string description, int categoryId, bool isActive, int purchaseId)
        {
            dbProfile.UpdateUserPurchase(title, description, categoryId, isActive, purchaseId);
        }

        /// <summary>
        /// Binds the data of a purchase, based on the "pid" QueryString.
        /// </summary>
        private void BindData()
        {
            Purchase pur = dbExchange.GetPurchaseDetails(Convert.ToInt32(Request.QueryString["pid"]));

            ddlEditPurchaseCategory.SelectedValue = pur.Category.CategoryID.ToString();
            chkBoxEditPurchaseStatus.Checked = pur.IsActive;
            chkBoxEditPurchaseStatus.Text = (pur.IsActive) ? "Aktiv" : "Inaktiv";
            txtEditPurchaseTitle.Text = pur.Title;
            ckeditorEditPurchaseDescription.Text = pur.Description;
        }

        protected void btnEditPurchase_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblConfirmUpdatedPurchase.Visible = true;

                string title = txtEditPurchaseTitle.Text;
                string description = ckeditorEditPurchaseDescription.Text;
                int categoryId = Convert.ToInt32(ddlEditPurchaseCategory.SelectedValue);
                bool isActive = chkBoxEditPurchaseStatus.Checked;
                int purchaseId = Convert.ToInt32(Request.QueryString["pid"]);

                EditPurchase(title, description, categoryId, isActive, purchaseId);
            }
            
        }

        protected void btnEditPurchaseCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/User/userPurchase.aspx?uid=" + Membership.GetUser().ProviderUserKey);
        }

        //Event fired when the Checked property of the checkbox is changed
        protected void chkBoxEditPurchaseStatus_CheckedChanged(object sender, EventArgs e)
        {
            chkBoxEditPurchaseStatus.Text = (chkBoxEditPurchaseStatus.Checked) ? "Aktiv" : "Inaktiv";
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnEditPurchase.Text = GetLocalResourceObject("btnEditPurchaseText").ToString();
            btnEditPurchaseCancel.Text = GetLocalResourceObject("btnEditPurchaseCancelText").ToString();

            hyLinkChangePassword.Text = GetLocalResourceObject("hyLinkChangePasswordText").ToString();
            hyLinkEditProfile.Text = GetLocalResourceObject("hyLinkEditProfileText").ToString();
            hyLinkProfile.Text = GetLocalResourceObject("hyLinkProfileText").ToString();
            hyLinkPurchases.Text = GetLocalResourceObject("hyLinkPurchasesText").ToString();
            hyLinkSale.Text = GetLocalResourceObject("hyLinkSaleText").ToString();
            hyLinkSettings.Text = GetLocalResourceObject("hyLinkSettingsText").ToString();

            lblEditPurchaseCategory.Text = GetLocalResourceObject("lblEditPurchaseCategoryText").ToString();
            lblEditPurchaseDescription.Text = GetLocalResourceObject("lblEditPurchaseDescriptionText").ToString();
            lblEditPurchaseHeader.Text = GetLocalResourceObject("lblEditPurchaseHeaderText").ToString();
            lblEditPurchaseStatus.Text = GetLocalResourceObject("lblEditPurchaseStatusText").ToString();
            lblEditPurchaseTitle.Text = GetLocalResourceObject("lblEditPurchaseTitleText").ToString();
            lblConfirmUpdatedPurchase.Text = GetLocalResourceObject("lblConfirmUpdatedPurchaseText").ToString();

            string descriptionValidation = GetLocalResourceObject("rfvalEditPurchaseDescriptionText").ToString();
            rfvalEditPurchaseDescription.ToolTip = descriptionValidation;
            rfvalEditPurchaseDescription.ErrorMessage = descriptionValidation;

            string TitleValidation = GetLocalResourceObject("rfvalEditPurchaseTitleText").ToString();
            rfvalEditPurchaseTitle.ToolTip = TitleValidation;
            rfvalEditPurchaseTitle.ErrorMessage = TitleValidation;

            string categoryValidation = GetLocalResourceObject("rfvalEditPurchaseCategoryText").ToString();
            rfvalEditPurchaseCategory.ToolTip = categoryValidation;
            rfvalEditPurchaseCategory.ErrorMessage = categoryValidation;

            valsumEditPurchase.HeaderText = GetLocalResourceObject("valsumEditPurchaseText").ToString();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorEditPurchaseDescription.Language = "en";
            else
                ckeditorEditPurchaseDescription.Language = "da";
        }
    }
}